package com.ms.pda.components

import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import com.blankj.utilcode.util.ActivityUtils
import com.ms.pda.R

class HeaderBar : Toolbar, View.OnClickListener {
    private var mTitle: TextView? = null
    private var mBackIcon: ImageView? = null

    constructor(context: Context) : super(context) {
        addViews(context)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        addViews(context)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        addViews(context)
    }

    private fun addViews(context: Context) {
        mTitle = TextView(context)
        mTitle!!.layoutParams = LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.MATCH_PARENT,
            Gravity.CENTER
        )
        mTitle!!.setTextSize(TypedValue.COMPLEX_UNIT_PT, 36f)
        mTitle!!.setTextColor(context.getColor(R.color.text_color))
        mTitle!!.gravity = Gravity.CENTER
        addView(mTitle)
        mBackIcon = ImageButton(context)
        mBackIcon!!.layoutParams =
            LayoutParams(100, ViewGroup.LayoutParams.MATCH_PARENT)
        mBackIcon!!.setBackgroundColor(context.getColor(android.R.color.transparent))
        mBackIcon!!.setImageResource(R.mipmap.back)
        mBackIcon!!.setOnClickListener(this)
        mBackIcon!!.scaleType = ImageView.ScaleType.CENTER
        addView(mBackIcon)
        setBackgroundResource(R.drawable.bg_input_bottom_line)
    }

    override fun onClick(v: View?) {
        ActivityUtils.finishActivity(ActivityUtils.getTopActivity())
    }
}